package com.zilue.payment.gateway.web.controller;

import com.zilue.payment.gateway.manage.GatewayManager;
import com.zilue.payment.gateway.manage.SignManager;
import com.zilue.payment.gateway.method.BaseMethod;
import com.zilue.payment.gateway.paramter.ParameterCheckResult;
import com.zilue.payment.gateway.paramter.PublicParameter;
import com.zilue.payment.gateway.reurndata.PublicReturn;
import com.zilue.payment.gateway.reurndata.enums.RequestStatusEnum;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by zilue on 2017/3/6.
 */
@RestController
@RequestMapping("payment")
public class GatewayController {
    private Logger logger = Logger.getLogger(GatewayController.class);

    @Autowired
    private SignManager signManager;

    @Autowired
    private GatewayManager gatewayManager;

    /**
     * 支付统一网关入口
     * （此处进行调用认证和公共参数校验）
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "gateway.do", method = RequestMethod.POST, produces = "application/json")
    public PublicReturn gateway(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //1.参数获取
        PublicParameter publicParamter = gatewayManager.getPublicParamter(request);
        if (publicParamter == null) {
            return PublicReturn.createFailedReturn(RequestStatusEnum.RequestBodyIllegal);
        }

        //2.公共参数校验
        ParameterCheckResult parameterCheckResult = publicParamter.ParameterCheck();
        if (!parameterCheckResult.isStatus()) {
            PublicReturn failedReturn = PublicReturn.createFailedReturn(RequestStatusEnum.ParameterIllegel);
            failedReturn.setMsg(parameterCheckResult.getDescription());
            return failedReturn;
        }

        //3.接口校验（可拓展接口权限校验）
        String method = publicParamter.getMethod();
        BaseMethod bean = gatewayManager.getBean(method, request);
        if (bean == null) {
            return PublicReturn.createFailedReturn(RequestStatusEnum.RequestNoSuchMethod);
        }

        //4.签名校验
        int verifyRequest = signManager.verifyRequest(publicParamter);
        if (verifyRequest == SignManager.NO_APPID) {
            return PublicReturn.createFailedReturn(RequestStatusEnum.AppidIllegal);
        } else if (verifyRequest == SignManager.AUTH_FAILED) {
            return PublicReturn.createFailedReturn(RequestStatusEnum.RequestSignFailed);
        }

        /*通过了基础的请求校验，即可认为请求成功*/
        return gatewayManager.proccess(publicParamter);

    }

    /**
     * 浏览器GET方式请求
     *
     * @return
     */
    @RequestMapping("gateway")
    public PublicReturn gateway() {
        return PublicReturn.createSuccessReturn(RequestStatusEnum.NotSupportGET);
    }


}
